package com.jcl.recruit.attach.service;

import com.jcl.core.ftp.FtpProxy;
import com.jcl.recruit.attach.logic.UploadFileLogic;
import com.jcl.recruit.attach.model.UploadFileInfo;
import com.jcl.recruit.core.common.FileFtpProxyFactory;
import com.jcl.core.constants.AttachmentConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @ClassName AttachmentService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/10/24 15:00
 * @Version 1.0
 **/
@Service
public class AttachmentService {

    @Autowired
    private UploadFileLogic uploadFileLogic;

    public byte[] headByte(String id){
        UploadFileInfo uploadFileInfo = uploadFileLogic.findOne(id);

        FtpProxy ftpProxy = null;
        try {
            byte[] b = null;
            ftpProxy = FileFtpProxyFactory.getFtpProxy();
            String newName = uploadFileInfo.getFilePath() + AttachmentConstant.SPLIT +
                    uploadFileInfo.getExternalName();
            b = ftpProxy.download(newName);
            return b;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != ftpProxy){
                ftpProxy.closeServer();
            }
        }
        return null;
    }

}
